package com.esl.action.admin;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.net.URLEncoder;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

import org.apache.struts2.ServletActionContext;

import com.esl.action.CommonAction;
import com.esl.model.admin.Resource;
import com.esl.model.admin.User;
import com.esl.service.admin.ResourceManager;
import com.esl.service.admin.UserManager;
import com.esl.util.SaeUtil;
import com.opensymphony.xwork2.ActionSupport;
import com.sina.sae.storage.SaeStorage;
import com.sina.sae.util.SaeUserInfo;

/**
 * 多文件上传，用集合的方式
 * 
 * @author Administrator
 * 
 */

public class UploadAction extends CommonAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = -4482490535762758007L;
	private String usename;
	private List<File> file1 = new ArrayList<File>();;
	private List<String> file1FileName = new ArrayList<String>();;
	private List<String> file1ContentType = new ArrayList<String>();
	private String dn = "";
	private String brief;
	private ResourceManager resourceManager;
	private UserManager userManager;

	private SaeUtil saeUtil;

	public String upload() throws Exception {

		for (int i = 0; i < file1.size(); i++) {

			Resource resource = save2Sae(file1.get(i), dn, file1FileName.get(i));
			resourceManager.add(resource);
		}

		return "toIndex";
	}

	/*------------------------------------------------*/

	protected Resource save2Sae(File file, String domainname, String fileName)
			throws IOException {
		
		SaeStorage storage = new SaeStorage();
		InputStream is = new FileInputStream(file);
		// 因为sae的storage限定是10MB，file的尺寸不会超int的表示范围
		byte[] buf = new byte[(int) file.length()];

		is.read(buf);
		String id = createid(9999);
		String fileType = fileName.substring(fileName.lastIndexOf(".")+1,fileName.length());
		
		storage.write(domainname, id+"."+fileType, buf);
		
		
		String url = storage.getUrl(domainname, id);
		
		User user=userManager.getUserByID(getUid());
		Resource resource =new Resource(id, fileName, url,fileType, brief,domainname,user);
		is.close();

		return resource;

	}
	/*--------------以下是实验方法----------------*/
	protected void save4FileWrap(File file, String domainname, String fileName)
			throws IOException {
		SaeStorage storage = new SaeStorage();
		InputStream is = new FileInputStream(file);
		// 因为sae的storage限定是10MB，file的尺寸不会超int的表示范围
		byte[] buf = new byte[(int) file.length()];

		is.read(buf);
		String fullname = "saestor://" + domainname + "/" + fileName;
		FileOutputStream os = new FileOutputStream(fullname);
		Writer writer = new OutputStreamWriter(os);
		String str = new String(buf);
		writer.write(str);
		
		os.close();
		writer.close();
		is.close();
	}

	protected void save4TmpFS(File file, String domainname, String fileName)
			throws IOException {
		String realPath = SaeUserInfo.getSaeTmpPath() + "/";
		FileOutputStream fos = new FileOutputStream(realPath + "/" + fileName);

		FileInputStream fis = new FileInputStream(file);
		byte[] buffer = new byte[1024 * 16];
		int len = 0;
		while ((len = fis.read(buffer)) > 0) {
			fos.write(buffer, 0, len);
		}

		SaeStorage ss = new SaeStorage();
		ss.upload(domainname, realPath + fileName, fileName);
	}

	protected String test(String str) {
		String str1[] = new String[] { "US-ASCII", "ISO-8859-1", "UTF-8",
				"UTF-16BE", "UTF-16LE", "UTF-16", "GBK" };
		String sum = "";
		for (String string : str1) {
			try {
				String s1 = new String(str.getBytes(), string);
				if (s1.equals(str)) {
					sum += string + "   ";
				}

			} catch (UnsupportedEncodingException e) {
				sum += "[error:" + string + "]   ";
				e.printStackTrace();
			}
		}
		return sum.toString();

	}

	protected String createid(int number) {

		Random random = new Random();
		return random.nextInt(number)
				+ Long.toString(System.currentTimeMillis())
				+ random.nextInt(number);
	}

	/*------------------------------------------------*/
	public String getUsename() {
		return usename;
	}

	public void setUsename(String usename) {
		this.usename = usename;
	}

	public List<File> getFile1() {
		return file1;
	}

	public void setFile1(List<File> file1) {
		this.file1 = file1;
	}

	public List<String> getFile1FileName() {
		return file1FileName;
	}

	public void setFile1FileName(List<String> file1FileName) {
		this.file1FileName = file1FileName;
	}

	public List<String> getFile1ContentType() {
		return file1ContentType;
	}

	public void setFile1ContentType(List<String> file1ContentType) {
		this.file1ContentType = file1ContentType;
	}

	public String getDn() {
		return dn;
	}

	public void setDn(String dn) {
		this.dn = dn;
	}

	/*------------------------------------------------*/
	@javax.annotation.Resource
	public void setResourceManager(ResourceManager resourceManager) {
		this.resourceManager = resourceManager;
	}

	@javax.annotation.Resource
	public void setUserManager(UserManager userManager) {
		this.userManager = userManager;
	}

	@javax.annotation.Resource
	public void setSaeUtil(SaeUtil saeUtil) {
		this.saeUtil = saeUtil;
	}

	/*------------------------------------------------*/

	public String getBrief() {
		return brief;
	}

	public void setBrief(String brief) {
		this.brief = brief;
	}

}
